#include "PCF8563T.h"
#include "i2c.h"

/*声明结构体变量****************************************************************/
Datetime_Structure Datetime;


/********************************************************************************
函数名称：u8 rtc8563_read(u8 rtc_addr)
功    能：读取指定地址的数据
输    入：rtc_addr：地址
输    出：无
*********************************************************************************/
uint8_t rtc8563_read(uint8_t rtc_addr)
{
		uint8_t rev_data;
	
	  HAL_I2C_Mem_Read(&hi2c1, ADDR_PCF8563, (uint16_t)rtc_addr, I2C_MEMADD_SIZE_8BIT, &rev_data, 1, 0x10);
	
	  return rev_data;	
}



/********************************************************************************
函数名称：void rtc_set_time(u8 year,u8 month,u8 day,u8 hour,u8 min,u8 second)
功    能：设置RTC芯片PCF8563的时间
输    入：year month day hour min second:年月日时分秒
输    出：无
*********************************************************************************/
void rtc_set_time(uint8_t year,uint8_t month,uint8_t day,uint8_t hour,uint8_t min,uint8_t second)
{

	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_SECOND, I2C_MEMADD_SIZE_8BIT, &second, 1, 0x10);
	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_MIN,    I2C_MEMADD_SIZE_8BIT, &min,    1, 0x10);
	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_HOUR,   I2C_MEMADD_SIZE_8BIT, &hour,   1, 0x10);
	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_DAY,    I2C_MEMADD_SIZE_8BIT, &day,    1, 0x10);
	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_MONTH,  I2C_MEMADD_SIZE_8BIT, &month,  1, 0x10);
	  HAL_I2C_Mem_Write(&hi2c1, ADDR_PCF8563, ADDR_YEAR,   I2C_MEMADD_SIZE_8BIT, &year,   1, 0x10);
		
}



/********************************************************************************
函数名称：Datetime_Structure rtc_get_time(void)
功    能：读取RTC芯片PCF8563的时间
输    入：无
输    出：Datetime_Structure结构体变量
*********************************************************************************/
Datetime_Structure rtc_get_time(void)
{
		uint32_t m_unTempTime;
		Datetime_Structure getTime_Struct;
		
		/*get time---second****************/
		m_unTempTime = rtc8563_read(ADDR_SECOND)&0x7f;
		m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.second = m_unTempTime;
	  
		/*get time---minute****************/
		m_unTempTime = rtc8563_read(ADDR_MIN)&0x7f;
    m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.min = m_unTempTime;
    
		/*get time---hour****************/
		m_unTempTime = rtc8563_read(ADDR_HOUR)&0x3f;
    m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.hour = m_unTempTime;
    
		/*get time---day****************/
		m_unTempTime = rtc8563_read(ADDR_DAY)&0x3f;
    m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.day = m_unTempTime;	
    
		/*get time---month****************/
		m_unTempTime = rtc8563_read(ADDR_MONTH)&0x1f;
    m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.month = m_unTempTime;		
		
		/*get time---year****************/
		m_unTempTime = rtc8563_read(ADDR_YEAR)&0xff;
    m_unTempTime = (m_unTempTime/16)*10+m_unTempTime%16;
		getTime_Struct.year = m_unTempTime;		
	
		return getTime_Struct;
}


